package com.wits.qso.dao.impl;

import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.wits.platform.dao.impl.BaseDaoImpl;
import com.wits.qso.dao.custom.CreContentDaoCustom;
import com.wits.qso.entity.Aid;
import com.wits.qso.entity.Btable;
import com.wits.qso.entity.CreContent;
import com.wits.qso.entity.HomeworkH;
import com.wits.qso.entity.MaterialD;

@Component
public class CreContentDaoImpl extends BaseDaoImpl<CreContent, String> implements CreContentDaoCustom
{

	public Aid findAidByBtable(Btable btable) {
		Aid result = null;
		try {
			Query query = em.createQuery( "SELECT DISTINCT o.aidId FROM CreContent o WHERE o.btId = :btable" );
			query.setParameter( "btable", btable );
			query.setMaxResults( 1 );

			result = (Aid) query.getSingleResult();
		} catch (NoResultException cause) {
		}

		return result;
	}

	public HomeworkH findHomeworkHByBtable(Btable btable) {
		HomeworkH result = null;

		try {
			Query query = em.createQuery( "SELECT DISTINCT o.hwkhId FROM CreContent o WHERE o.btId = :btable" );
			query.setParameter( "btable", btable );
			query.setMaxResults( 1 );

			result = (HomeworkH) query.getSingleResult();
		} catch (NoResultException cause) {
		}

		return result;
	}

	public CreContent findCreContentByBtable(Btable btable) {
		CreContent result = null;

		try {
			Query query = em.createQuery( "SELECT DISTINCT o FROM CreContent o WHERE o.btId = :btable" );
			query.setParameter( "btable", btable );
			query.setMaxResults( 1 );

			result = (CreContent) query.getSingleResult();
		} catch (NoResultException cause) {
		}

		return result;
	}

	public List<MaterialD> findMaterialDByBtable(Btable btable) {
		List<MaterialD> result = null;
		
		try {
			Query query = em.createQuery( "SELECT o.mtrdId FROM CreContent o WHERE o.btId = :btable" );
			query.setParameter( "btable", btable );

			result = (List<MaterialD>) query.getResultList();
		} catch (NoResultException cause) {
		}
		
		return result;
	}

	public List<CreContent> findByBtable(Btable btable) {
		Query query = em.createQuery( "FROM CreContent o WHERE o.btId = :btable" );
		query.setParameter( "btable", btable );

		return (List<CreContent>) query.getResultList();
	}

	public int deleteByBtable(Btable btable) {
		Query query = em.createQuery( "DELETE FROM CreContent o WHERE o.btId = :btable" );
		query.setParameter( "btable", btable );

		return query.executeUpdate();
	}
}
